﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCSService;
using System.Text;
using SCS.Core.Models;
using SCS.Core.ViewModels;


namespace SCS.Core.Controllers
{
    public class PostController : BaseFontendController
    {

        #region "ListCategory"

        public ActionResult CategoryDetail(int id)
        {
            var type = Models.ModelsFontend.PostModel.GetCategory(id);
            ViewData["CategoryID"] = id;
            return View(type);
        }

        public StringBuilder GetListCategoryBuilder(int? id)
        {
            StringBuilder sb = new StringBuilder();
            return ListCategoryBuilder(ref sb, id);
        }

        public StringBuilder ListCategoryBuilder(ref StringBuilder sb, int? id)
        {
            int siteGuid=0;
            if (Request.Url.Host == "localhost")
            {
                //siteGuid = 2;
                if (Request.Url.ToString().Contains("favicon.ico"))
                {

                }
                else
                    siteGuid = DefaultSiteGuidGetter();
            }
            else
                siteGuid = Models.ConfigModel.GetWebsitebyDomain(DefaultHostGetter()).SiteGuid;

            List<SCSService.Post_Category> listCate = Models.ModelsFontend.PostModel.GetListCategoryByParentId(id);
            sb.Append("<ul>");
            foreach (var item in listCate)
            {
                if (DefaultHostGetter().Contains("localhost"))
                {
                    sb.Append("<li><a href='/" + siteGuid + "/" + DefaultLanguageGetter() + "/Post/ListPost/" + item.CategoryID + "'>" + item.CategoryName + "</a></li>");
                }
                else
                {
                    sb.Append("<li><a href='/" + DefaultLanguageGetter() + "/Post/ListPost/" + item.CategoryID + "'>" + item.CategoryName + "</a></li>");
                }
               

                List<SCSService.Post_Category> listChild = Models.ModelsFontend.PostModel.GetListCategoryByParentId(item.CategoryID);
                if (listChild.Count() > 0)
                {
                    ListCategoryBuilder(ref sb, item.CategoryID);
                }
                sb.Append("</li>");
            }
            sb.Append("</ul>");

            return sb;
        }

        #endregion

        #region "Post"

        public ActionResult PostDetail(int id)
        {
            var type = Models.ModelsFontend.PostModel.GetPostByID(id);
            ViewData["IDPost"] = id;
            ViewData["ListCategory"] = GetListCategoryBuilder(null);
           

            return View(type);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ListPost(int? id)
        {
            //Id đầu vào là cateId
            int currentPage = 1;
            int pageSize = 2000;

            ViewData["currentPage"] = currentPage;
            ViewData["pageSize"] = pageSize;

            return ListPost(currentPage, pageSize, null, id);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ListPost(int? currentPage, int? pageSize, string gridAction, int? id)
        {
            ViewData["ListCategory"] = GetListCategoryBuilder(null);

            //id đầu vào là typeId
            if (gridAction == "PageSizeChanged")
                currentPage = 1;

            if (currentPage == null && gridAction == "CurrentPageChanged")
            {
                string strCurrentPage = Request.Form.Get("quickPage").Replace(",", "");
                currentPage = int.Parse(strCurrentPage);
            }
            else if (currentPage == null)
            {
                currentPage = 1;
            }

            if (pageSize == null)
            {
                pageSize = 2000;
            }

            ViewData["currentPage"] = currentPage;


            var query = Models.ModelsFontend.PostModel.GetListPostByCategory(id).AsQueryable();
            int totalRows = query.Count();

            int totalPages = (int)Math.Ceiling((double)totalRows / (double)pageSize);
            if (totalPages != 1)
            {
                //paging
                query = query.Skip(((int)currentPage - 1) * (int)pageSize)
                                .Take((int)pageSize);
            }

            UpdatePagerViewData(totalPages, totalRows, (int)currentPage, (int)pageSize,null);

            var result = query.ToList();

            if (query.Count() > 0)
            {

            }

            if (totalRows == 0)
                return View(new List<SCSService.Post>());
            else
                return View(result);
        }

        #endregion

    }
}
